package com.note.utils;

import java.util.Random;


//生成验证码
public class CodeUtils {

    private static char[] numbers = "0123456789".toCharArray();

    private static char[] words = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ".toCharArray();

    private static final int MIN_LEN = 4;

    private static final int MAX_LEN = 8;

    //数字验证码
    public static String generateNumber(int len) {
        len = limitLen(len);
        Random random = new Random();
        char[] cs = new char[len];
        for (int i = 0; i < cs.length; i++) {
            cs[i] = numbers[random.nextInt(numbers.length)];
        }
        return new String(cs);
    }

    //返回字符数字混合型验证码
    public static String generateCode(int len) {
        len = limitLen(len);
        Random random = new Random();
        char[] cs = new char[len];
        for (int i = 0; i < cs.length; i++) {
            cs[i] = words[random.nextInt(words.length)];
        }
        return new String(cs);
    }

    //限制验证码长度
    private static int limitLen(int len) {
        if (len < MIN_LEN) {
            return MIN_LEN;
        } else if (len > MAX_LEN) {
            return MAX_LEN;
        } else {
            return len;
        }
    }

    public static void main(String[] args) {
        String code=CodeUtils.generateCode(6);
        System.out.println("code===="+code);
        String  number=CodeUtils.generateNumber(6);
        System.out.println("number===="+number);
    }
}